package acm.europe2005.southwestern;

import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;


public class p2403 {

	static int N, from, to;
	static int[][] al;
	static int[] level;
	static Queue<Integer> queue;

	public static void main(String[] args) throws Exception {
//		Scanner scanner = new Scanner(new File("tests/03.in"));
		Scanner scanner = new Scanner(System.in);
		N = scanner.nextInt();
		al = new int[N][];
		for (int i=0;i<N;++i) {
			int id, nNei;
			id = scanner.nextInt();
			nNei = scanner.nextInt();
			al[id] = new int[nNei];
			for (int j=0;j<nNei;++j) {
				al[id][j] = scanner.nextInt();
			}
		}
		from = scanner.nextInt();
		to = scanner.nextInt();
		level = new int[N];
		for (int i=0;i<N;++i) {
			level[i] = -1;
		}
		level[from] = 0;
		queue = new ArrayBlockingQueue<Integer>(N);
		queue.add(from);
		boolean stop = false;
		while (!queue.isEmpty()) {
			int fr = queue.poll();
			for (int i=0;i<al[fr].length;++i) {
				if (al[fr][i] == to) {
					System.out.println(from + " " + to + " " + level[fr]);
					stop = true;
					break;
				}
				if (level[al[fr][i]] == -1) {
					level[al[fr][i]] = level[fr] + 1;
					queue.add(al[fr][i]);
				}
			}
			if (stop) {
				break;
			}
		}
	}
	
}
